<meta charset="utf-8">
(#) Using the QUERY_ALL_PACKAGES permission

!!! ERROR: Using the QUERY_ALL_PACKAGES permission
   This is an error.

Id
:   `QueryAllPackagesPermission`
Summary
:   Using the QUERY_ALL_PACKAGES permission
Severity
:   Error
Category
:   Compliance
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   4.1.0 (October 2020)
Affects
:   Manifest files
Editing
:   This check runs on the fly in the IDE editor
See
:   https://g.co/dev/packagevisibility
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/PackageVisibilityDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PackageVisibilityDetectorTest.kt)

If you need to query or interact with other installed apps, you should
be using a `<queries>` declaration in your manifest. Using the
QUERY_ALL_PACKAGES permission in order to see all installed apps is
rarely necessary, and most apps on Google Play are not allowed to have
this permission.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
AndroidManifest.xml:6:Error: A <queries> declaration should generally be
used instead of QUERY_ALL_PACKAGES; see
https://g.co/dev/packagevisibility for details
[QueryAllPackagesPermission]
    &lt;uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/&gt;&lt;!-- ERROR --&gt;
                     ----------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`AndroidManifest.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
&lt;?xml version="1.0" encoding="utf-8"?&gt;
                &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.pkg"&gt;
                    &lt;uses-sdk android:targetSdkVersion="30"/&gt;
                    &lt;!-- breaks manifest merging &lt;uses-permission/&gt; --&gt;&lt;!-- Test for NPEs --&gt;
                    &lt;uses-permission android:name="android.permission.CAMERA"/&gt;&lt;!-- OK --&gt;
                    &lt;uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/&gt;&lt;!-- ERROR --&gt;
                &lt;/manifest&gt;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/MainActivity.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import android.app.Activity
import android.content.Intent
import android.os.Bundle

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val context = applicationContext
        val pm = context.packageManager

        pm.getInstalledPackages(0) // ERROR
        pm.getInstalledApplications(0) // ERROR

        pm.queryBroadcastReceivers(Intent(), 0) // ERROR
        pm.queryContentProviders("", 0, 0) // ERROR
        pm.queryIntentServices(Intent(), 0) // ERROR
        pm.queryIntentActivities(Intent(), 0) // ERROR

        Intent().resolveActivity(pm) // ERROR
        Intent().resolveActivityInfo(pm, 0) // ERROR

        this.getInstalledPackages() // OK
        this.resolveActivity() // OK
    }

    private fun getInstalledPackages() = Unit
    private fun resolveActivity() = Unit
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PackageVisibilityDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `PackageVisibilityDetector.testCanQueryAllPackages`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Adding the suppression attribute
  `tools:ignore="QueryAllPackagesPermission"` on the problematic XML
  element (or one of its enclosing elements). You may also need to add
  the following namespace declaration on the root element in the XML
  file if it's not already there:
  `xmlns:tools="http://schemas.android.com/tools"`.

  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;manifest xmlns:tools="http://schemas.android.com/tools"&gt;
      ...
      &lt;uses-permission tools:ignore="QueryAllPackagesPermission" .../&gt;
    ...
  &lt;/manifest&gt;
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="QueryAllPackagesPermission" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'QueryAllPackagesPermission'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore QueryAllPackagesPermission ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>